from es import setindexprop, getindexprop, getindexfromhandle, getplayerprop, doblock, server
from spe import registerPreHook, unregisterPreHook
from weaponlib import getWeapon
from superdice.superdice import registerRoll, unregisterRoll, players
from superdice.libs.playerExtras import getPlayer

def unload():
    doblock('corelib/noisy_off')
    unregisterPreHook('weapon_fire', weapon_fire_pre)
    unregisterRoll(diceRoll)

def player_spawn(ev):
    players[ev['userid']]['wpngain'] = False

def weapon_fire_pre(ev):
    userid = str(ev['userid'])
    weapon = ev['weapon'].lower()
    if not players[userid]['wpngain']:
        return

    if weapon in ('hegrenade', 'smokegrenade', 'flashbang'):
        getPlayer(userid).givePlayer('weapon_' + weapon)
        return

    setindexprop(getindexfromhandle(getplayerprop(userid, 'CBaseCombatCharacter.m_hActiveWeapon')), 'CBaseCombatWeapon.LocalWeaponData.m_iClip1', getWeapon(weapon).clip)

def diceRoll(userid, player, eplayer):
    players[userid]['wpngain'] = True

doblock('corelib/noisy_on')

registerPreHook('weapon_fire', weapon_fire_pre)

registerRoll(diceRoll, 'infammo')
